//
//  subViewForResults.m
//  HondaPro_2
//
//  Created by Eric on 13-7-9.
//  Copyright (c) 2013年 glendale. All rights reserved.
//

#import "subViewForResults.h"
#import "UserDefinedCellForubViewForResults.h"

@interface subViewForResults ()

//创建控件
-(void)createStaticLabels;
-(void)createTableView;

//刷新数据
-(void)freshenViewDis;

@end

@implementation subViewForResults
@synthesize tableViewDis = _tableViewDis;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        [self createStaticLabels];
        [self createTableView];
    }
    return self;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    
    [self freshenViewDis];
}

#pragma mark - 创建控件

-(void)createStaticLabels
{
    NSArray *arrForAllKeys = [NSArray arrayWithObjects:
                              @"bottomLbl",
                              @"statementNumber",
                              @"stateContent", nil];
    
    NSDictionary *dicForStaticLabelsRect = [NSDictionary dictionaryWithObjectsAndKeys:
                                            [NSValue valueWithCGRect:CGRectMake(0, 0, self.frame.size.width, 50)],@"bottomLbl",
                                            [NSValue valueWithCGRect:CGRectMake(0, 0, 80, 50)],@"statementNumber",
                                            [NSValue valueWithCGRect:CGRectMake(81, 0, self.frame.size.width-81, 50)],@"stateContent", nil];
    
    NSDictionary *dicForStaticLabelsText = [NSDictionary dictionaryWithObjectsAndKeys:
                                            @"",@"bottomLbl",
                                            @"序号",@"statementNumber",
                                            @"用户陈述",@"stateContent", nil];
    
    for (int i = 0; i<[arrForAllKeys count]; i++)
    {
        NSString *strKeys = [arrForAllKeys objectAtIndex:i];
        CGRect rect = (CGRect)[[dicForStaticLabelsRect objectForKey:strKeys] CGRectValue];
        UILabel *lbl = [[UILabel alloc] init];
        lbl.frame = rect;
        if ([strKeys isEqualToString:@"bottomLbl"])
        {
            //            lbl.backgroundColor = [UIColor grayColor];
            //将灰色背景色修改成浅灰色
            lbl.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
        }
        else
        {
            //将灰色背景色修改成浅灰色
            lbl.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
//            lbl.backgroundColor = [UIColor redColor];
        }
        lbl.textColor = [UIColor grayColor];
        lbl.font = [UIFont systemFontOfSize:16];
        lbl.text = [dicForStaticLabelsText objectForKey:strKeys];
        [self addSubview:lbl];
        
        if ([strKeys isEqualToString:@"statementNumber"])
        {
            lbl.textAlignment = UITextAlignmentCenter;
        }
    }
}

-(void)createTableView
{
    self.tableViewDis = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, self.frame.size.width, self.frame.size.height-50)];
    self.tableViewDis.backgroundColor = [UIColor clearColor];
    
    self.tableViewDis.dataSource = self;
    self.tableViewDis.delegate = self;
    
    [self addSubview:self.tableViewDis];
}

#pragma mark - 刷新数据

-(void)freshenViewDis
{
    [self.tableViewDis reloadData];
}

#pragma mark - tableView dataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    
    return 3 + [modelIns.arrForResults count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    UserDefinedCellForubViewForResults *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (nil == cell)
    {
        cell = [[UserDefinedCellForubViewForResults alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    //cell的风格是：浅浅灰色和浅灰色间隔出现，第一行是浅灰色
    NSInteger row = [indexPath row];
    switch (row%2)
    {
        case 0:
            //浅灰色
            cell.contentView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
            break;
        case 1:
            //浅浅灰色
            cell.contentView.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
            break;
            
        default:
            break;
    }
    
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    
    if (row<[modelIns.arrForResults count])
    {
        NSDictionary *dicTmp = [modelIns.arrForResults objectAtIndex:row];
        ((UILabel *)[cell.dicForAllLabelsOnCell objectForKey:@"statementNumber"]).text = [dicTmp objectForKey:@"statementNumber"];
        ((UILabel *)[cell.dicForAllLabelsOnCell objectForKey:@"stateContent"]).text = [dicTmp objectForKey:@"stateContent"];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
    }
    else
    {
        ((UILabel *)[cell.dicForAllLabelsOnCell objectForKey:@"statementNumber"]).text = @"";
        ((UILabel *)[cell.dicForAllLabelsOnCell objectForKey:@"stateContent"]).text = @"";
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    return cell;
}

#pragma mark - tableView delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = [indexPath row];
    
    //在此 将业务模型中对应的属性 设定为 当前选中的为第几行
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    
    if (row>=[modelIns.arrForResults count])
    {
        modelIns.nRowForResults = -1;
    }
    else
    {
        modelIns.nRowForResults = row;
    }
    
    //设置其他页面选择行 为 默认选择行 －1
    modelIns.nRowForIndicationList = -1;
    modelIns.nRowForRepairItemList = -1;
    modelIns.nRowForPartList = -1;
    modelIns.nRowForOtherChargeList = -1;
    
    //所有的tableView都要reloadData,通过发送消息实现
    
    //给 作业指示 类 发送消息
    [[NSNotificationCenter defaultCenter] postNotificationName:INDICATION_LIST_MSG_ object:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:REPAIR_ITEM_LIST_MSG_ object:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:PART_LIST_MSG object:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:OTHER_CHARGE_LIST_MSG object:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:ADD_BTN_MSG_ object:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:MODIFY_BTN_MSG_ object:nil];
}

@end
